var tgd = tgd || {};
tgd.Utilities = tgd.Utilities || {};
tgd.Utilities.Stopwatch = tgd.Utilities.Stopwatch || {};

tgd.Utilities.Stopwatch.Timer = function (update) {
	this._running = false;
	this._elapsedMS = 0;
	this._lastStarted = 0;

	this.onUpdate = update;
	

	if (tgd.Utilities.Stopwatch.Timer._initialized === undefined) {

		//todo:
		tgd.Utilities.Stopwatch.Timer.prototype.start = function() {
			var self = this;

			if (!self._running) {
				self._running = true;

				this._lastStarted = new Date() - this._elapsedMS;

				var runTimer = function() {
					self.onUpdate();

					if (self._running && self.onUpdate) {
						setTimeout(runTimer, 32);
					} else {
						//todo:
					}
				};
				setTimeout(runTimer, 32);
			}
		}

		tgd.Utilities.Stopwatch.Timer.prototype.stop = function() {
			this._running = false;
			this._elapsedMS = new Date() - this._lastStarted;
			this._lastStarted = 0;  //todo: don't know yet
		}

		tgd.Utilities.Stopwatch.Timer.prototype.reset = function() {
			this._running = false;
			//todo: ???
		}

		//todo:
		tgd.Utilities.Stopwatch.Timer.prototype.toString = function(forTitle) {
			var now = new Date();
			return now - this._elapsedMS;  //todo: don't know yet
		}

		tgd.Utilities.Stopwatch.Timer._initialized = true;
	}
};